package cn.sczhyt.modules.szhy.service.impl;

import cn.sczhyt.common.utils.R;
import cn.sczhyt.modules.szhy.entity.MeetingTopicsEntity;
import cn.sczhyt.modules.szhy.service.MeetingTopicsService;
import cn.sczhyt.modules.szhy.service.MeetingUserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.Query;

import cn.sczhyt.modules.szhy.dao.MeetingVoteDao;
import cn.sczhyt.modules.szhy.entity.MeetingVoteEntity;
import cn.sczhyt.modules.szhy.service.MeetingVoteService;
import org.springframework.transaction.annotation.Transactional;


@Service("meetingVoteService")
public class MeetingVoteServiceImpl extends ServiceImpl<MeetingVoteDao, MeetingVoteEntity> implements MeetingVoteService {

    @Autowired
    private MeetingTopicsService meetingTopicsService;
    @Autowired
    private MeetingVoteDao meetingVoteDao;
    @Autowired
    MeetingUserService meetingUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MeetingVoteEntity> page = this.page(
                new Query<MeetingVoteEntity>().getPage(params),
                new QueryWrapper<MeetingVoteEntity>()
        );
        return new PageUtils(page);
    }

    //表决结果保存
    @Transactional(rollbackFor = Exception.class)
    @Override
    public R saveVote(MeetingVoteEntity meetingVoteEntity) {

        MeetingTopicsEntity meetingTopicsEntity = meetingTopicsService.getById(meetingVoteEntity.getTopicsId());

        System.out.println(meetingTopicsEntity);

        if (meetingTopicsEntity.getIfAnonymous() == 1 || meetingTopicsEntity.getIfAnonymous() == 2) {

            if (meetingTopicsEntity.getIfTopics() == 1) {
                MeetingVoteEntity vote = meetingVoteDao.selectByVote(meetingVoteEntity.getMeetingId(), meetingVoteEntity.getTopicsId(), meetingVoteEntity.getUserId());
                if (vote == null) {
                    meetingVoteDao.insert(meetingVoteEntity);
                    return R.ok();
                }
                return R.error(201, "你投过票了哈");
            } else if (meetingTopicsEntity.getIfTopics() == 2) {

                return R.error(202, "投票杀果了");
            }
            return R.error(203, "不在表决时间内哈哈哈哈");
        }

        return R.error(204, "此议题不支持表决噢噢噢噢");


    }

    @Override
    public JSONObject voteCount(String meetingId, String topicsId) {
        System.out.println("meetingId" + meetingId + "topicsId" + topicsId);
        Integer favor = meetingVoteDao.voteCount(meetingId, topicsId, 0);
        Integer opposition = meetingVoteDao.voteCount(meetingId, topicsId, 1);
        Integer waiver = meetingVoteDao.voteCount(meetingId, topicsId, 2);
        Integer count = meetingUserService.userCount(meetingId);
        JSONObject object = new JSONObject();

        object.put("favor",favor);
        object.put("opposition",opposition);
        object.put("waiver",waiver);
        object.put("count",count);
        System.out.println(object.toString());
        return object;
    }

    @Override
    public List<MeetingVoteEntity> findByTopicsId(String meetingId, String topicsId) {

        return meetingVoteDao.findByTopicsId(meetingId,topicsId);
    }

}
